package com.longrise.co.ui.home.presenter;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carlife.model.NewsBean;
import com.longrise.co.ui.home.contract.HomeContract;
import com.longrise.co.ui.home.model.AreaIdBean;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.ui.home.model.CarCompany;
import com.longrise.co.ui.home.model.CompanyDetail;
import com.longrise.co.utils.Constants;
import com.ly.common.baserx.RxSubscriber;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-6-23.
 */

public class HomePresenter extends HomeContract.Presenter {

    @Override
    public void getCarCompany(int pageNum, int pageSize, String orgName, String level, String sortFlag, boolean isShowProcess) {
        try {
            EntityBean bean = new EntityBean();

            if (!StringUtils.isEmpty(orgName)) {
                bean.set("orgname", orgName);
            }

            if (!StringUtils.isEmpty(level)) {
                bean.set("level", level);
            }

            if (!StringUtils.isEmpty(sortFlag)) {
                bean.set("sortflag", sortFlag);
            }

            bean.set("pagenum", pageNum);
            bean.set("pagesize", pageSize);

//            bean.set("areaid","410101000000000000");
//            String s = EnCryptDeCrypt2.getInstance().EnCrypt("410101000000000000");
//            bean.set("sign",s);
            if(CommonPara.getInstance().isLogin()){
                String areaid = CommonPara.getInstance().getUserAreaid();
                bean.set("areaid",areaid);
                bean.set("sign",EnCryptDeCrypt2.getInstance().EnCrypt(areaid));
            }else {
                String areaid = CommonPara.getInstance().getDefaultAreaid();
                bean.set("areaid", areaid);
                bean.set("sign", EnCryptDeCrypt2.getInstance().EnCrypt(areaid));
            }
            bean.set("user", CommonPara.getInstance().getUser());


            mRxManage.add(mModel.carCompanyRequest(bean).subscribe(new RxSubscriber<CarCompany>(mContext, isShowProcess) {
                @Override
                protected void _onNext(CarCompany bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete(Constants.HOME_LOAD_CARSERVICE);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getBindCarList(String carNo) {
        try {
            EntityBean bean = new EntityBean();

            if(!StringUtils.isEmpty(carNo)) {
                bean.set("carno", carNo);
            }
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.bindCarListRequest(bean).subscribe(new RxSubscriber<BindCarInfo>(mContext, false) {
                @Override
                protected void _onNext(BindCarInfo bean) {
                    if(mView instanceof HomeContract.HomeView) {
                        ((HomeContract.HomeView) mView).backBindCarList(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete(Constants.HOME_LOAD_CAR);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getCompanyDetail(String companyId, String areaId) {
        try {
            EntityBean bean = new EntityBean();
            if(StringUtils.isEmpty(companyId) || StringUtils.isEmpty(areaId)) {
                return ;
            }
            String enCompanyId = EnCryptDeCrypt2.getInstance().EnCrypt(companyId);

            bean.set("companyid", companyId);
            bean.set("areaid", areaId);
            bean.set("sign", enCompanyId);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.companyDetailRequest(bean).subscribe(new RxSubscriber<CompanyDetail>(mContext, true) {
                @Override
                protected void _onNext(CompanyDetail bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete(-1);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getHomeNewsList(int pageNum, int pageSize, boolean isShowProcess) {
        try {

            EntityBean bean = new EntityBean();

            bean.set("areaid", CommonPara.getInstance().getAreaId());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("pagesize", String.valueOf(pageSize));
            bean.set("pagenum", pageNum);
            bean.set("sign", CommonPara.getInstance().getEnAreaId());

            mRxManage.add(mModel.getHomeNewsRequest(bean).subscribe(new RxSubscriber<NewsBean>(mContext, isShowProcess) {
                @Override
                protected void _onNext(NewsBean bean) {
                    if(mView instanceof HomeContract.HomeView) {
                        ((HomeContract.HomeView) mView).backHomeNewsList(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete(Constants.HOME_LOAD_NEWS);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getNearByCompanyList(EntityBean bean, boolean isShowProcess) {
        try {
          /*  EntityBean bean = new EntityBean();

            if (!StringUtils.isEmpty(orgName)) {
                bean.set("orgname", orgName);
            }

            if (!StringUtils.isEmpty(level)) {
                bean.set("level", level);
            }

            if (!StringUtils.isEmpty(sortFlag)) {
                bean.set("sortflag", sortFlag);
            }

            bean.set("pagenum", pageNum);
            bean.set("pagesize", pageSize);

            bean.set("areaid", CommonPara.getInstance().getAreaId());
            bean.set("sign", CommonPara.getInstance().getEnAreaId());
            bean.set("user", CommonPara.getInstance().getUser());*/


            mRxManage.add(mModel.nearByCompanyRequest(bean).subscribe(new RxSubscriber<CarCompany>(mContext, isShowProcess) {
                @Override
                protected void _onNext(CarCompany bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete(Constants.HOME_LOAD_CARSERVICE);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getCityAreaId(String city, boolean isShowProcess) {
        try {

            if(StringUtils.isEmpty(city)) {
                mView.showErrorTip("切换城市失败");
                return ;
            }
            EntityBean bean = new EntityBean();
            String enCity = EnCryptDeCrypt2.getInstance().EnCrypt(city);

            bean.set("cityname", city);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enCity);
            mRxManage.add(mModel.cityAreaIdRequest(bean).subscribe(new RxSubscriber<AreaIdBean>(mContext, isShowProcess) {
                @Override
                protected void _onNext(AreaIdBean bean) {
                    if(mView instanceof HomeContract.HomeView) {
                        ((HomeContract.HomeView) mView).backAreaId(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }
}
